package com.example.gulimallcoupon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.example.common.to.MemberPrice;
import com.example.common.to.SkuReductionTO;
import com.example.common.utils.R;
import com.example.gulimallcoupon.entity.MemberPriceEntity;
import com.example.gulimallcoupon.entity.SkuLadderEntity;
import com.example.gulimallcoupon.service.MemberPriceService;
import com.example.gulimallcoupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimallcoupon.dao.SkuFullReductionDao;
import com.example.gulimallcoupon.entity.SkuFullReductionEntity;
import com.example.gulimallcoupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public R saveSkuFullReduction(SkuReductionTO skuFullReductionTO) {
        // 1.保存满减打折、会员价
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuFullReductionTO,skuLadderEntity);
        skuLadderEntity.setAddOther(skuFullReductionTO.getCountStatus());
        skuLadderService.save(skuLadderEntity);
        // 2.保存满减
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuFullReductionTO,skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(skuFullReductionTO.getPriceStatus());
        save(skuFullReductionEntity);
        // 3.保存会员价格
        List<MemberPrice> memberPrices = skuFullReductionTO.getMemberPrice();
        if(CollectionUtil.isNotEmpty(memberPrices)){
            List<MemberPriceEntity> collect = memberPrices.stream().map(item -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuFullReductionTO.getSkuId());
                memberPriceEntity.setMemberLevelId(item.getId());
                memberPriceEntity.setMemberLevelName(item.getName());
                memberPriceEntity.setMemberPrice(item.getPrice());
                memberPriceEntity.setAddOther(1);
                return memberPriceEntity;
            }).filter(memberPriceEntity -> memberPriceEntity.getMemberPrice().compareTo(new BigDecimal(0)) > 0).toList();
            memberPriceService.saveBatch(collect);
        }
        return R.ok();
    }

}